﻿#ifndef __USART_H__
#define __USART_H__


#include <stdint.h>
#include "gd32f4xx.h"

typedef struct
{
    rcu_periph_enum rcu_periph; /*!< 使能时钟 */
    uint32_t        baudrate;   /*!< 波特率 */
    uint32_t        databits;   /*!< 数据位 */
    uint32_t        parity;     /*!< 校验位 */
    uint32_t        stopbits;   /*!< 停止位 */
}UsartContig_t;

typedef struct UsartType
{
    uint32_t          usartx;               /*!< 外设号 */
    uint8_t          *recive_buffer[2];     /*!< 接收缓冲区 */
    uint32_t          recive_buffer_size;   /*!< 接收缓冲区大小 */
    volatile uint32_t recive_len;           /*!< 接收数据长度 */
    volatile uint8_t  recive_full_ptr;      /*!< 接收缓冲区满指针,
                                                 0: 无数据, 1: 缓冲区0满, 2: 缓冲区1满 */
    volatile uint8_t  recive_wirt_ptr;      /*!< 接收缓冲区写指针,
                                                 0: 缓冲区0, 1: 缓冲区1 */
}UsartType_t;

#define USART_INSTANCE_MAX 5


int Usart_Constructor(UsartType_t *instance, uint32_t usartx, uint32_t recive_buffer_size);
int Usart0_Init(const UsartType_t *instance, const UsartContig_t *config);
int USART_Send(const UsartType_t *instance, uint8_t *data, uint32_t size, uint32_t timeout);
int USART_Read(UsartType_t *instance, uint8_t *data, uint32_t *size);

#endif // USART_H

